//
//  AutoSizeTableViewCell.m
//  TouGuApp
//
//  Created by tianshikechuang on 16/7/11.
//  Copyright © 2016年 tianshikechuang. All rights reserved.
//

#import "AutoSizeTableViewCell.h"

@interface AutoSizeTableViewCell ()
{
    
}
//用户人头像
@property(nonatomic, strong) UIImageView * iconImage;
//昵称
@property(nonatomic, strong) UILabel * nameLabel;
//热度的按钮
@property(nonatomic, strong) UIButton * hotBut;
//文字信息
@property(nonatomic, strong) UILabel * contentLabel;
//图片
@property(nonatomic, strong) UIImageView * contentImages;
//点赞的按钮
@property(nonatomic, strong) UIButton * zangBut;
//评论
@property(nonatomic, strong) UIButton * pinLunBtn;
//分享
@property(nonatomic, strong) UIButton * fenXianBtn;
//收藏
@property(nonatomic, strong) UIButton * souCanBtn;
//尾视图
@property (nonatomic, strong) UIView * footView;
//时间
@property(nonatomic,strong)UILabel * timeLab;


@end


@implementation AutoSizeTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithTableView:(UITableView *)tableView
{
    static NSString * identf = @"cell";
    self = [tableView dequeueReusableCellWithIdentifier:identf];
    
    if (!self) {
        self = [[AutoSizeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identf];
    
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self addHeadView];
        [self addBodyView];
        [self addFooterView];
    }
    
    return self;
}


- (void)addHeadView{
    
    UIView * HeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, WidthOfView([UIScreen mainScreen]), 60)];
    [self addSubview:HeadView];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconTapAction:)];
    
    UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconTapAction:)];

    
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 60, 60)];
    
    imageView.layer.cornerRadius = 30;
    imageView.clipsToBounds = YES;
    imageView.backgroundColor = [UIColor greenColor];
    imageView.userInteractionEnabled = YES;
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(90, 15, 120, 30)];
    label.userInteractionEnabled = YES;
    [imageView addGestureRecognizer:tap];
    [label addGestureRecognizer:tap1];
    
    UIButton * hot = [[UIButton alloc] initWithFrame:CGRectMake(WidthOfView([UIScreen mainScreen]) - 90, 15, 80, 30)];
    [hot setImage:[UIImage imageNamed:@"pt"] forState:UIControlStateNormal];
    hot.userInteractionEnabled = NO;
    
    //增加时间label
    UILabel * timeLab = [[UILabel alloc]initWithFrame:CGRectMake(WidthOfView([UIScreen mainScreen]) - 150, 15, 150, 30)];
    timeLab.font = [UIFont systemFontOfSize:13];
//    timeLab.backgroundColor = [UIColor blueColor];
    self.timeLab = timeLab;
    [self addSubview:timeLab];
    
    self.iconImage = imageView;
    self.nameLabel = label;
    self.hotBut = hot;
    
    [HeadView addSubview:imageView];
    [HeadView addSubview:label];
    [HeadView addSubview:hot];
}

- (void)addBodyView{
    
    UIView * bodyView = [[UIView alloc] initWithFrame:CGRectMake(0, 80, WidthOfView([UIScreen mainScreen]), 130)];
    [self addSubview:bodyView];
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, WidthOfView([UIScreen mainScreen]) - 20, 60)];
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor grayColor];
    [bodyView addSubview:label];
    self.contentLabel = label;
    
    
    UIImageView * imagesview = [[UIImageView alloc] initWithFrame:CGRectMake(10, 70,WidthOfView([UIScreen mainScreen]) - 20, 60)];
    imagesview.backgroundColor = [UIColor grayColor];
    [bodyView addSubview:imagesview];
    self.contentImages = imagesview;
    
    
}

- (void)addFooterView{
    
    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(WidthOfView([UIScreen mainScreen]) - 180 - 20, 230, 250, 40)];
    [self addSubview:footView];
    
    
    UIButton * button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 69, 30)];
    [button1 setImage:[UIImage imageNamed:@"icon_n1"] forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button1 addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
        if (self.clikBut) {
            self.clikBut(1);
        }
        
    }];
    
    UIButton * button2 = [[UIButton alloc] initWithFrame:CGRectMake(70, 0, 69, 30)];
    [button2 setImage:[UIImage imageNamed:@"icon_n2"] forState:UIControlStateNormal];
    [button2 addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
        if (self.clikBut) {
            self.clikBut(2);
        }
        
    }];
    
    
    UIButton * button3 = [[UIButton alloc] initWithFrame:CGRectMake(140, 0, 69, 30)];
    [button3 setImage:[UIImage imageNamed:@"icon_n4"] forState:UIControlStateNormal];
    [button3 addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
        if (self.clikBut) {
            self.clikBut(3);
        }
        
    }];
    
//    UIButton * button4 = [[UIButton alloc] initWithFrame:CGRectMake(210, 0, 30, 30)];
//    [button4 setImage:[UIImage imageNamed:@"icon_n4"] forState:UIControlStateNormal];
    
    
    self.zangBut = button1;
    self.pinLunBtn = button2;
    self.fenXianBtn = button3;
//    self.souCanBtn = button4;
    
    [footView addSubview:button1];
    [footView addSubview:button2];
    [footView addSubview:button3];
//    [footView addSubview:button4];
    
    self.footView = footView;
}


- (void)setModel:(AutoSizeModel *)model{
    
    _model = model;

    
    
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"--"]];
    
    self.nameLabel.text = model.name;
    self.hotBut.hidden = !model.isHot;
    
    
    self.contentLabel.frame = CGRectMake(10, 0, model.contentTextSize.width,model.contentTextSize.height);
    self.contentLabel.font = model.contentTextFont;
    self.contentLabel.text = model.contentText;
    self.contentLabel.backgroundColor = [UIColor greenColor];
    
    self.timeLab.text = model.p_date;
    self.timeLab.hidden = model.isHot;
    
    
    
    //给文本内容添加一个手势
    UITapGestureRecognizer * textTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textTapAction:)];
    
    [self.contentLabel addGestureRecognizer:textTap];
    
    //允许text与用户进行交互
    self.contentLabel.userInteractionEnabled = YES;
    

    self.contentImages.frame = CGRectMake(10, model.contentTextSize.height + 10, model.contentImageSize.width,model.contentImageSize.height);
    
    
    
    [self.contentImages.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if (model.contentImages.count > 0) {
        
        CGFloat width = (model.contentImageSize.width - 20) / 3.0;
        CGFloat height = model.contentImageSize.width/3.0;
        CGFloat gap = 10;
        [model.contentImages enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if (idx >= 3) {
                return ;
            }
            
            NSURL * url = [NSURL URLWithString:[model.contentImages objectAtIndex:idx]];
            UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(idx * (width + gap), 0, width, height)];
            [image sd_setImageWithURL:url];
            image.clipsToBounds = YES;
            image.contentMode = UIViewContentModeScaleAspectFill;
            [self.contentImages addSubview:image];
        }];
//        [self.contentImages sd_setImageWithURL:[NSURL URLWithString:[model.contentImages firstObject]]];
//        self.contentImages.contentMode = UIViewContentModeScaleAspectFill;
//        self.contentImages.clipsToBounds = YES;
    }
    
    
    if (model.contentText.length == 0) {
        self.contentLabel.backgroundColor = [UIColor grayColor];
    }else{
        self.contentLabel.backgroundColor = [UIColor clearColor];
    }
    
    if (model.contentImages.count == 0) {
        self.contentImages.backgroundColor = [UIColor grayColor];
    }else{
        self.contentImages.backgroundColor = [UIColor clearColor];
    }

    [self addImages:model.contentImages];

    CGFloat y = model.totalHeight - 40;
    
    self.footView.frame = CGRectMake(WidthOfView([UIScreen mainScreen]) - 180 -20, y, 180, 40);

    [self.zangBut setTitle:model.Zang forState:UIControlStateNormal];
    [self.pinLunBtn setTitle:model.Pinlun forState:UIControlStateNormal];
//    [self.fenXianBtn setTitle:model.Fenxian forState:UIControlStateNormal];
    
}

-(void)addTime:(NSDate *)time{
    
    
    
    
}



- (void)addImages:(NSArray *)images{
    
    NSLog(@"%s",__func__);
    
    
}



#pragma mark - Action
- (void)iconTapAction:(UITapGestureRecognizer *)tap{
    self.iconBlock();
}


-(void)textTapAction:(UITapGestureRecognizer *)tap{
    
    self.textBlock();
    
}

//
//
//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    
//    [super setSelected:selected animated:animated];
//
////   self.nameLabel.text = @"adf";
//   
//    // Configure the view for the selected state
//}

@end
